package com.anis.project.medcare.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import com.anis.jpa.medical.model.MedicalReport;
import com.anis.jpa.medical.model.Patient;
import com.anis.project.medcare.Activator;
import com.anis.project.medcare.dicom.file.selector.DicomFileSelector;
import com.anis.project.medcare.editors.PatientEditor;
import com.anis.project.medcare.editors.RdvEditor;
import com.anis.project.medcare.editors.ReportEditor;
import com.anis.project.medcare.editors.input.PatientEditorInput;
import com.anis.project.medcare.editors.input.RdvEditorInput;
import com.anis.project.medcare.editors.input.ReportEditorInput;
import com.anis.project.medcare.search.dialog.SearchPatientDialog;
import com.anis.project.medcare.search.dialog.SearchReportDialog;

import org.eclipse.wb.swt.ResourceManager;

public class ChoicesView extends ViewPart {

	public static final String ID = "com.anis.project.medcare.view.choices";

	public ChoicesView() {
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout(SWT.VERTICAL));

		Button btnaddPAtient = new Button(parent, SWT.CENTER);
		btnaddPAtient.setTouchEnabled(true);
		btnaddPAtient.setImage(ResourceManager.getPluginImage(
				"com.anis.project.medcare", "icons/adduser.png"));

		Button btnSearchPatient = new Button(parent, SWT.BORDER | SWT.CENTER);
		btnSearchPatient.setImage(ResourceManager.getPluginImage(
				"com.anis.project.medcare", "icons/search.png"));

		Button btnAppointement = new Button(parent, SWT.NONE);
		btnAppointement.setImage(ResourceManager.getPluginImage(
				"com.anis.project.medcare", "icons/date.png"));

		Button btnpatientReport = new Button(parent, SWT.NONE);
		btnpatientReport.setImage(ResourceManager.getPluginImage(
				"com.anis.project.medcare", "icons/report_check.png"));

		Button btnNewReport = new Button(parent, SWT.BORDER | SWT.TOGGLE
				| SWT.CENTER);
		btnNewReport.setImage(ResourceManager.getPluginImage(
				"com.anis.project.medcare", "icons/new_doc.png"));

		Button btnDicomViewer = new Button(parent, SWT.BORDER | SWT.TOGGLE
				| SWT.CENTER);
		btnDicomViewer.setImage(ResourceManager.getPluginImage(
				"com.anis.project.medcare", "icons/dcm.png"));

		btnSearchPatient.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Shell parentShell = Activator.getDefault().getWorkbench()
						.getActiveWorkbenchWindow().getShell();
				SearchPatientDialog searchDialog = new SearchPatientDialog(
						parentShell);
				searchDialog.open();
			}
		});

		btnAppointement.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DicomViewer dicomViewer = (DicomViewer) Activator.getDefault()
						.getWorkbench().getActiveWorkbenchWindow()
						.getActivePage().findView(DicomViewer.ID);
				if (dicomViewer != null)
					Activator.getDefault().getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.hideView(dicomViewer);
				RdvEditorInput rdvEditorInput = new RdvEditorInput();
				try {
					Activator.getDefault().getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.openEditor(rdvEditorInput, RdvEditor.ID);
				} catch (PartInitException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});

		btnpatientReport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DicomViewer dicomViewer = (DicomViewer) Activator.getDefault()
						.getWorkbench().getActiveWorkbenchWindow()
						.getActivePage().findView(DicomViewer.ID);
				if (dicomViewer != null)
					Activator.getDefault().getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.hideView(dicomViewer);
				Shell parentShell = Activator.getDefault().getWorkbench()
						.getActiveWorkbenchWindow().getShell();
				SearchReportDialog searchDialogReport = new SearchReportDialog(
						parentShell);
				if (searchDialogReport != null)
					searchDialogReport.open();

			}
		});

		btnaddPAtient.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DicomViewer dicomViewer = (DicomViewer) Activator.getDefault()
						.getWorkbench().getActiveWorkbenchWindow()
						.getActivePage().findView(DicomViewer.ID);
				if (dicomViewer != null)
					Activator.getDefault().getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.hideView(dicomViewer);
				Patient patient = new Patient();
				PatientEditorInput patientEditorInput = new PatientEditorInput(
						patient);

				try {
					Activator.getDefault().getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.openEditor(patientEditorInput, PatientEditor.ID);
				} catch (PartInitException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		btnNewReport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DicomViewer dicomViewer = (DicomViewer) Activator.getDefault()
						.getWorkbench().getActiveWorkbenchWindow()
						.getActivePage().findView(DicomViewer.ID);
				if (dicomViewer != null)
					dicomViewer.dispose();
				MedicalReport radioReport = new MedicalReport();
				ReportEditorInput radioReportInput = new ReportEditorInput(
						radioReport);
				try {
					Activator.getDefault().getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.openEditor(radioReportInput, ReportEditor.ID);
				} catch (PartInitException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		btnDicomViewer.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Shell parentShell = Activator.getDefault().getWorkbench()
						.getActiveWorkbenchWindow().getShell();
				DicomFileSelector dicomFileSelector = new DicomFileSelector(
						parentShell);
				if (dicomFileSelector != null)
					dicomFileSelector.open();
			}
		});
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {

	}
}